package com.itheima.test0909.test04;

import java.util.Scanner;

public class binarySeachForIndex {
    public static void main(String[] args) {
        int[] arr = {10,22,33,44,55,66,77,88,99,100};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要查找的整数");
        int i = scanner.nextInt();

        int result = getIndex(i,arr);
        if (result == -1){
            System.out.println("需查找元素位置不存在");
        }else {
            System.out.println("需查找元素位置为:" + result);
        }

    }

    private static int getIndex(int i, int[] arr) {
        int min = 0;
        int max = arr.length-1;
        int mid = -1;
        while(min <= max){
            mid = (max + min) / 2;
            if (arr[mid] == i){
                return mid;
            }else if(arr[mid] < i){
                min = mid + 1;
            }else if (arr[mid] > i){
                max = mid - 1;
            }
        }
        return -1;
    }
}
